package com.fr.web.core.service;

import com.fr.base.BaseUtils;
import com.fr.base.ClassFinder;
import com.fr.base.FRContext;
import com.fr.base.core.BaseCoreUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.DateUtils;
import com.fr.base.core.FRCoreContext;
import com.fr.base.core.RSADecode;
import com.fr.base.core.ReservedWords;
import com.fr.base.core.json.JSONObject;
import com.fr.base.dav.Env;
import com.fr.report.script.core.FArray;
import com.fr.util.Consts;
import com.fr.web.cache.WebletFactory;
import com.fr.web.core.ShowWorkBookPolicy.Policy4Page;
import com.fr.web.core.WebUtils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AppServerConfigService extends NoSessionIDService
{
  private static AppServerConfigService APPSERVERCONFIG_SERVICE = new AppServerConfigService();

  public static AppServerConfigService getInstance()
  {
    return APPSERVERCONFIG_SERVICE;
  }

  public boolean accept(String paramString)
  {
    return ((paramString.equals("appserver")) || (paramString.equals("keywords")) || ("appjar".equals(paramString)));
  }

  public void process(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String paramString)
    throws Exception
  {
    if (paramString.equalsIgnoreCase("appserver"))
      dealWithServer(paramHttpServletRequest, paramHttpServletResponse);
    else if (paramString.equalsIgnoreCase("keywords"))
      dealWithKeywords(paramHttpServletRequest, paramHttpServletResponse);
    else if ("appjar".equalsIgnoreCase(paramString))
      dealWithAppJar(paramHttpServletRequest, paramHttpServletResponse);
  }

  private static void dealWithAppJar(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str1 = "com.fr.util.Consts";
    if (WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "class") != null)
      str1 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "class");
    FArray localFArray1 = new FArray();
    ClassFinder localClassFinder = new ClassFinder(str1);
    String str2 = FRContext.getCurrentEnv().getPath();
    String str3 = new File(str2, "classes").getAbsolutePath();
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    localFArray1.add(str3);
    localClassFinder.findClass(str3, true);
    str3 = new File(str2, "lib").getAbsolutePath();
    localFArray1.add(str3);
    localClassFinder.findClass(str3, true);
    str3 = AppletPrintService.class.getResource("").getPath();
    if (new File(str3).getName().equals("service"))
      str3 = new File(str3).getParentFile().getParentFile().getParentFile().getParentFile().getParent();
    localFArray1.add(str3);
    List localList = localClassFinder.findClass(str3, true);
    HashMap localHashMap = new HashMap();
    localHashMap.put("searchList", localFArray1);
    localHashMap.put("class", str1);
    FArray localFArray2 = new FArray();
    HashSet localHashSet = new HashSet(localList);
    Iterator localIterator = localHashSet.iterator();
    while (localIterator.hasNext())
      localFArray2.add(localIterator.next().toString());
    localHashMap.put("resultsize", new Integer(localFArray2.length()));
    localHashMap.put("result", localFArray2);
    ReportletDealWith.dealWithReportlet(paramHttpServletRequest, paramHttpServletResponse, WebletFactory.createEmbeddedReportlet("/com/fr/web/console/appjar.cpt", new ShowWorkBookPolicy.Policy4Page(), localHashMap));
  }

  private static void dealWithServer(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    HashMap localHashMap = new HashMap();
    byte[] arrayOfByte = FRCoreContext.getBytes();
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    try
    {
      RSADecode.decode(arrayOfByte, localByteArrayOutputStream);
    }
    catch (Exception localException1)
    {
    }
    try
    {
      JSONObject localJSONObject = new JSONObject(new String(localByteArrayOutputStream.toByteArray(), "UTF-8"));
      if ((localJSONObject.has("version")) && (ComparatorUtils.equals(Consts.VERSION, localJSONObject.getString("version"))) && (localJSONObject.has("deadline")))
      {
        localHashMap.put("Registered", "true");
        localHashMap.put("Concurrency", localJSONObject.get("concurrency"));
        localHashMap.put("MACAddress", (localJSONObject.has("macaddress")) ? localJSONObject.get("macaddress") : "Temporary License");
        localObject = new Date(localJSONObject.getLong("deadline"));
        if (DateUtils.subtractDate((Date)localObject, new Date(), "d") < 365L)
        {
          localHashMap.put("OverdueDate", localObject);
          localHashMap.put("FormalRegistered", "false");
        }
      }
    }
    catch (Exception localException2)
    {
    }
    localHashMap.put("JARVersion", Consts.APP_NAME + Consts.VERSION);
    localHashMap.put("JARCreateTime", BaseUtils.readBuildNo());
    Runtime localRuntime = Runtime.getRuntime();
    localHashMap.put("FreeMemory", new Long(localRuntime.freeMemory() / 1024L / 1024L) + "M");
    localHashMap.put("TotalMemory", new Long(localRuntime.totalMemory() / 1024L / 1024L) + "M");
    localHashMap.put("MaxMemory", new Long(localRuntime.maxMemory() / 1024L / 1024L) + "M");
    Object localObject = BaseCoreUtils.getMacAddresses();
    localHashMap.put("ALLMACAddress", new FArray((String[])((List)localObject).toArray(new String[((List)localObject).size()])));
    ReportletDealWith.dealWithReportlet(paramHttpServletRequest, paramHttpServletResponse, WebletFactory.createEmbeddedReportlet("/com/fr/web/console/appServer.cpt", new ShowWorkBookPolicy.Policy4Page(), localHashMap));
  }

  private static void dealWithKeywords(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    FArray localFArray = new FArray();
    int i = 0;
    int j = ReservedWords.getReservedWordsCount();
    while (i < j)
    {
      localFArray.add(ReservedWords.getReservedWords(i).getKeyWords() + ":" + ReservedWords.getReservedWords(i).getDescription());
      ++i;
    }
    HashMap localHashMap = new HashMap();
    localHashMap.put("keywords", localFArray);
    ReportletDealWith.dealWithReportlet(paramHttpServletRequest, paramHttpServletResponse, WebletFactory.createEmbeddedReportlet("/com/fr/web/console/keywords.cpt", new ShowWorkBookPolicy.Policy4Page(), localHashMap));
  }
}